import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/util/event_bus.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart' as FlutterStars;
import 'package:ls_work/common/common.dart';
import 'package:ls_work/util/sm4_utils.dart';
import 'package:base_flutter/base_flutter.dart' as b;

import '../../service/sos_warning_service.dart';
import '../user_permission_data.dart';

// const String EventUserLogin = 'EventUserLogin';
const String EventUserLogout = 'EventUserLogout';

class UserManager {
  static UserManager? _instance;

  static String cookieStr = "";
  static String userName = "";
  static int? humanId;
  static String pdd = "";
  static String account = "";
  static String itemKey = "";
  static String? lastUpdatePwdTime;

  static UserManager get instance {
    if (_instance == null) {
      _instance = UserManager();
    }
    return _instance!;
  }

  login(Map<dynamic, dynamic> userJson) {
    var users = User.fromJson(userJson);
    debugPrint('我是登录成功表示');
    FlutterStars.SpUtil.putString(Constant.phone, (users.phonenumber??"").sm4);
    FlutterStars.SpUtil.putString(Constant.userName, users.userCaption??"");
    if(userJson['account']!=null){
      FlutterStars.SpUtil.putString("acc", (userJson['account'] as String).sm4);
    }
    if(userJson['password']!=null){
      FlutterStars.SpUtil.putString("mm", ((userJson['password'] as String?)??""));
    }
    FlutterStars.SpUtil.putBool(Constant.login_islogin, true);
    FlutterStars.SpUtil.putInt(Constant.humanId, users.humanId??0);
    FlutterStars.SpUtil.putInt(Constant.userId, users.id??0);
    lastUpdatePwdTime = userJson['updatePasswordTime'];
    FlutterStars.SpUtil.putString(Constant.lastUpdatePwdTime, userJson['updatePasswordTime']??"");
    // FlutterStars.SpUtil.putString(Constant.cookie, userJson['cookie']);

    // FlutterStars.SpUtil.putString(Constant.cookie, users.sessionId);
    eventBus.emit(Constant.eventUserLogin);
  }

  String getCookie() {
    cookieStr = FlutterStars.SpUtil.getString(Constant.cookie)??"";

    return cookieStr;
  }

  void init() {
    FlutterStars.SpUtil.remove("phone");
    var savedCookie = FlutterStars.SpUtil.getString(Constant.cookie)??"";
    if(savedCookie.isNotEmpty){
      cookieStr = savedCookie.sm4Decode;
      b.ImageLoad.globalHeader = {"Cookie":cookieStr};
    }
    // print("cookie====>${cookieStr}");
    userName = FlutterStars.SpUtil.getString(Constant.userName)??"";
    var oldOdd = FlutterStars.SpUtil.getString("password")??"";
    if(oldOdd.isNotEmpty){
      FlutterStars.SpUtil.putString("mm", oldOdd);
      FlutterStars.SpUtil.remove('password');
      pdd = oldOdd;
      
    }else{
      var mm = FlutterStars.SpUtil.getString("mm")??"";
      if(mm.isNotEmpty){
        pdd = mm;
      }
    }

    var acc = FlutterStars.SpUtil.getString("acc")??"";
    if(acc.isNotEmpty){
      account = acc.sm4Decode;
    }
    debugPrint("account===>${account}");
    humanId = FlutterStars.SpUtil.getInt(Constant.humanId);
    itemKey = FlutterStars.SpUtil.getString(Constant.itemKey)??"";
    lastUpdatePwdTime = FlutterStars.SpUtil.getString(Constant.lastUpdatePwdTime);
  }

  saveCookie(Map<String, dynamic> userJson) async {
    cookieStr = userJson['cookie'];
    b.ImageLoad.globalHeader = {"Cookie":cookieStr};
    await FlutterStars.SpUtil.putString(Constant.cookie, (userJson['cookie'] as String).sm4);
    eventBus.emit(Constant.eventUserLogin);
  }

  void saveItemKey(String key) async {
    itemKey = key;
    await FlutterStars.SpUtil.putString(Constant.itemKey, key);
  }

  void logout() async {
    cookieStr = "";
    b.ImageLoad.globalHeader = null;
    FlutterStars.SpUtil.remove(Constant.humanId);
    FlutterStars.SpUtil.remove(Constant.login_islogin);
    FlutterStars.SpUtil.remove(Constant.userId);
    FlutterStars.SpUtil.remove(Constant.phone);
    // FlutterStars.SpUtil.remove(Constant.login_userName);
    // FlutterStars.SpUtil.remove(Constant.userName);
    FlutterStars.SpUtil.remove(Constant.cookie);
    // HttpUtil.cookieJar.deleteAll();
    UserPermissionData.clear();
    eventBus.emit(EventUserLogout);
    SosWarningService.instance.stopService();
  }
}

class User {
  String? sessionId;
  int? id;
  String? userName;
  String? logo;
  String? phonenumber;
  String? userCaption;
  int? humanId;
  int? tenantId;

  User.fromJson(Map json) {
    sessionId = json['sessionId'];
    id = json['id'];
    userName = json['userName'];
    logo = json['logo'];
    humanId = json['humanId'];
    tenantId = json['tenantId'];
    phonenumber = json['phonenumber'];
    userCaption = json['userCaption'];
  }

  Map<String, dynamic> toJson() {
    return {
      'logo': logo,
      'userName': userName,
      'id': id,
      'sessionId': sessionId,
      'humanId': humanId,
      'tenantId': tenantId,
      'phonenumber': phonenumber,
      'userCaption': userCaption,
    };
  }
}
